package io.zhifei.modules.sys.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.ApiResponse;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.modules.sys.system.dao.SysLiveDao;
import io.zhifei.modules.sys.system.entity.SysLive;
import io.zhifei.modules.sys.system.service.SysLiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class SysLiveImpl extends ServiceImpl<SysLiveDao, SysLive> implements SysLiveService {

    @Autowired
    private SysLiveDao sysLiveDao;

    @Override
    public ApiResponse addLive(SysLive sysLive, String pushAddress, String pullAddress) {
        //处理直播推流地址
        sysLive.setPushUrl(pushAddress + "/" + sysLive.getTeacherNumber());
        //处理直播拉流地址
        sysLive.setPlayUrl(pullAddress + "/" + sysLive.getTeacherNumber());
        //检查该课程还有没有还未直播的内容，如果有，则不能新增
        int liveCount = sysLiveDao.getLiveCount(sysLive.getCourseId());
        if (liveCount > 0) {
            return new ApiResponse<>("500", "该课程还有未直播的内容哟！");
        } else {
            sysLiveDao.insert(sysLive);
        }
        return new ApiResponse<>("200", "新增成功！");
    }

    @Override
    public PageUtils liveList(String type, Integer page, Integer pageSize) {
        if (!type.equals("true")) {
            type = null;
        }
        Page pageParam = new Page(page, pageSize);
        Page<Map<String, Object>> pageResult = sysLiveDao.getLiveList(pageParam, type);
        return new PageUtils(pageResult);
    }

    @Override
    public void deleteLive(String[] ids) {

        List<String> idList = Arrays.asList(ids);
        List<SysLive> lives = new ArrayList<>();
        //将数组转为带删除的直播
        for (String id : idList) {
            SysLive sysLive = new SysLive();
            sysLive.setId(Long.valueOf(id));
            sysLive.setFlag("1");
            lives.add(sysLive);
            sysLiveDao.updateById(sysLive);
        }
    }
}
